struct Solution;

impl Solution {
    pub fn max_vowels(s: String, k: i32) -> i32 {
        fn is_vowel(c: u8) -> bool {
            c == b'a' || c == b'e' || c == b'i' || c == b'o' || c == b'u'
        }

        let k = k as usize;
        let mut max = 0;
        let mut res = 0;
        let bytes = s.into_bytes();

        for (index, c) in bytes.iter().enumerate() {
            if is_vowel(*c) {
                res += 1;
            }

            if index >= k && is_vowel(bytes[index - k]) {
                res -= 1;
            }

            max = i32::max(res, max);
        }

        max
    }
}

#[cfg(test)]
mod test {
    use crate::Solution;

    #[test]
    fn test_01() {
        let s = String::from("aaaa");
        let k = 1;

        assert_eq!(Solution::max_vowels(s, k), 1);
    }

    #[test]
    fn test_02() {
        let s = String::from("ddddd");
        let k = 1;

        assert_eq!(Solution::max_vowels(s, k), 0);
    }

    #[test]
    fn test_03() {
        let s = String::from("abciiidef");
        let k = 3;

        assert_eq!(Solution::max_vowels(s, k), 3);
    }

    #[test]
    fn test_04() {
        let s = String::from("aeiou");
        let k = 2;

        assert_eq!(Solution::max_vowels(s, k), 2);
    }

    #[test]
    fn test_05() {
        let s = String::from("leetcode");
        let k = 3;

        assert_eq!(Solution::max_vowels(s, k), 2);
    }
}

fn main() {
    let s = String::from("leetcode");
    let k = 3;

    assert_eq!(Solution::max_vowels(s, k), 2);
}
